﻿/// <summary>
/// <Filename>: Motion_PIP.cs
/// Author: Jiang Xiaolong
/// Created: 2015.01.30
/// Version: 1.0
/// Company: Sunnytech
/// Function: 画中画表格运动信息处理类
///
/// Changed By:
/// Modification Time:
/// Discription:
/// <summary>
using UnityEngine;
using System;
using System.Collections.Generic;
using System.Data;
using System.Reflection;
using System.IO;
using System.Collections;
using Motion.MotionPlatform.Tools;

namespace Motion.MotionPlatform
{
	public class PIPInfoClass : IMotion
	{

		//默认为Active
		public CurrentState State
		{
			get { return _state; }
			set { _state = value; }
		}
		private CurrentState _state = CurrentState.Active;
		//时间参数
		//速率
		public float SpeedRate
		{
			get { return Motion_Data.SpeedRate; }
			set { Motion_Data.SpeedRate = value; }
		}
		//正常速率时间
		public float StandardTime
		{
			get { return _standardTime; }
			set { _standardTime = value; }
		}
		private float _standardTime = 0f;
		//运动开始时间
		public float StartTime
		{
			get { return _startTime; }
			set { _startTime = value; }
		}
		private float _startTime = 0f;
		//运动结束时间
		public float EndTime
		{
			get { return _endTime; }
			set { _endTime = value; }
		}
		private float _endTime = 0f;

		public string position;
		public float screen_size;
		public float screen_size2 = 0f;
		public string ref_obj;
		public Vector3 camera_pos;
		public Vector3 camera_elu;
		public float camera_size;
		public string focus_obj;
		public bool isDisappear;

		public bool isSquare;

		Transform parent;

		GameObject pip_obj = null;

		//结构初始化
		public void Init()
		{
			Type _t = Motion_TypeGet.MotionTypeGet("minmap");
			if (!MotionPlatformData.st_minmap && _t != null)
			{
				MotionPlatformData.st_minmap = (MonoBehaviour)GameObject.FindObjectOfType(_t);
			}
			if (MotionPlatformData.st_minmap && _t != null)
			{
				MethodInfo methodInfo = _t.GetMethod("RelectionSetPara");
				BindingFlags flag = BindingFlags.Public | BindingFlags.Instance;
				object[] para = new object[] { position, screen_size, screen_size2, isSquare };
				methodInfo.Invoke(MotionPlatformData.st_minmap, flag, Type.DefaultBinder, para, null);
			}

			pip_obj = GameObject.Find(MotionPlatformData.pip_camera);

			//画中画相机的位置，角度
			if (ref_obj != "")
			{//相对位置
				pip_obj.transform.parent = GameObject.Find(ref_obj).transform;
				pip_obj.transform.localPosition = camera_pos;
				pip_obj.transform.localEulerAngles = camera_elu;

				pip_obj.transform.parent = null;
			}
			else
			{ //绝对位置
				pip_obj.transform.position = camera_pos;
				pip_obj.transform.eulerAngles = camera_elu;
			}

			//画中画相机的视野
			//pip_obj.transform.position = camera_pos;
			//pip_obj.transform.eulerAngles = camera_elu;
			if (pip_obj.camera.isOrthoGraphic)
			{
				pip_obj.camera.orthographicSize = camera_size;
			}
			else
			{
				pip_obj.camera.fieldOfView = camera_size;
			}

			pip_obj.camera.enabled = true;

			//画中画相机的父子关系
			if (focus_obj != "")
			{
				parent = pip_obj.transform.parent;
				pip_obj.transform.parent = GameObject.Find(focus_obj).transform;
			}

			//画中画相机的位置，角度
			//pip_obj.transform.localPosition = camera_pos;
			//pip_obj.transform.localEulerAngles = camera_elu;
		}

		//暂停控制--通过MotionPara.PauseControl控制
		public void Pause(bool play_status)
		{

		}

		//速率控制
		public void ChangeRate(float current_rate, float current_time)
		{
			EndTime = current_time + (EndTime - current_time) * SpeedRate / current_rate;
		}

		//运动
		public void Move(float current_time, float speed_rate, float delta_time)
		{

		}

		//时间管理
		public bool TimesUp(float current_time)
		{
			if (current_time > EndTime)
				return true;
			else
				return false;
		}

		//后处理，主要是位置修正
		public void PostProcess()
		{
			//画中画消失
			if (isDisappear)
			{
				pip_obj.camera.enabled = false;
				if (focus_obj != "")
				{
					pip_obj.transform.parent = parent;
				}
			}
			else
			{

			}
		}
	}

	/// <summary>
	/// PIP运动信息提取管理类，相当于工厂；
	/// </summary>
	public class Motion_PIPInfoManager : IMotionInfoManager
	{
		/// <summary>
		/// 信息获取函数，用反射的方式创建Tips相应的解析类；
		/// </summary>
		/// <param name="motion_symbol">当前运动标识符；</param>
		/// <param name="data_row">当前传入的Excel数据行；</param>
		/// <param name="row_id">当前行对应ID号；</param>
		/// <param name="group_table">当前Group数据表；</param>
		/// <param name="motion_list">简单运动类列表；</param>
		/// <param name="complex_motion_list">复合运动类列表；</param>
		/// <returns>解析是否正确；</returns>
		public bool InfoGet(string motion_symbol, int state, DataRow data_row, string row_id, DataTable group_table, IList<IMotion> motion_list, IList<IList<IMotion>> complex_motion_list)
		{
			string motionTypeID = "ONLY";
			if (Motion_Data.MotionFunctionAllow(motion_symbol, motionTypeID, state))
			{
				if (Motion_Data.MotionFunctionClassDic[motion_symbol].ContainsKey(motionTypeID))
				{
					Type _t = Motion_TypeGet.MotionTypeGet("Motion.MotionPlatform." + Motion_Data.MotionFunctionClassDic[motion_symbol][motionTypeID]);
					//实例化相应的解析类
					IMotionFunctionInfo infoInstance = (IMotionFunctionInfo)Activator.CreateInstance(_t);

					////将Excel中参数转换成参数表里面的数据
					//Motion_Compute.ExcelDataAnalysis(ref data_row);
					return infoInstance.InfoGet(data_row, row_id, group_table, motion_list, complex_motion_list);
				}
				else
				{
					Debug.LogError("PIP运动标志符：" + motionTypeID + "，不含有该运动标志符对应的解析类，请询问相关程序猿！");
					return false;
				}
			}
			else
			{
				return true;
			}
		}
	}

	/// <summary>
	/// 引出线运动；
	/// </summary>
	public class Motion_PIP_Only : IMotionFunctionInfo
	{

		public bool InfoGet(DataRow data_row, string row_id, DataTable group_table, IList<IMotion> motion_list, IList<IList<IMotion>> complex_motion_list)
		{
			PIPInfoClass _pipMotion = new PIPInfoClass();

			bool is_right = true;
			Hashtable pipHashTable = new Hashtable();

			//屏幕位置
			string screen_position = (string)data_row[2].ToString().ToLower();
			if (screen_position == "right_up" || screen_position == "left_up" || screen_position == "right_down"
					|| screen_position == "left_down" || screen_position == "center")
			{
				_pipMotion.position = screen_position;
			}
			else
			{
				if (Motion_Data.isEditor)
				{
					Debug.LogError(ErrorLocation.Locate("PIP", "SCREEN_POSITION", row_id) + "，画中画屏幕位置出错！");
				}
			}
			pipHashTable.Add("Position", _pipMotion.position);

			//屏幕大小
			if (!data_row[3].ToString().Contains(","))
			{
				float screen_size = Motion_Compute.FloatConversion((string)data_row[3].ToString(), ref is_right);
				if (!is_right)
				{
					if (Motion_Data.isEditor)
					{
						Debug.LogError(ErrorLocation.Locate("PIP", "MOVESPEED", row_id) + "，画中画屏幕大小出错！");
					}
					return false;
				}
				_pipMotion.screen_size = screen_size;
				pipHashTable.Add("Size", _pipMotion.screen_size + "," + _pipMotion.screen_size2);
			}else{
				string[] tmpStrs = data_row[3].ToString().Split(',');

				float screen_size = Motion_Compute.FloatConversion(tmpStrs[0], ref is_right);
				if (!is_right)
				{
					if (Motion_Data.isEditor)
					{
						Debug.LogError(ErrorLocation.Locate("PIP", "MOVESPEED", row_id) + "，画中画屏幕大小出错！");
					}
					return false;
				}
				_pipMotion.screen_size = screen_size;

				float screen_size2 = Motion_Compute.FloatConversion(tmpStrs[1], ref is_right);
				if (!is_right)
				{
					if (Motion_Data.isEditor)
					{
						Debug.LogError(ErrorLocation.Locate("PIP", "MOVESPEED", row_id) + "，画中画屏幕大小出错！");
					}
					return false;
				}
				_pipMotion.screen_size2 = screen_size2;

				pipHashTable.Add("Size", _pipMotion.screen_size + "," + _pipMotion.screen_size2);
			}

			//显示时间
			float time = 0;
			if ((string)data_row[4].ToString() != "")
			{
				time = Motion_Compute.FloatConversion((string)data_row[4].ToString(), ref is_right);
				if (!is_right)
				{
					if (Motion_Data.isEditor)
					{
						Debug.LogError(ErrorLocation.Locate("PIP", "TIME", row_id) + "，画中画显示时间出错！");
					}
					return false;
				}
			}
			_pipMotion.StandardTime = time;
			_pipMotion.EndTime = _pipMotion.StandardTime / _pipMotion.SpeedRate;
			pipHashTable.Add("Time", _pipMotion.StandardTime);

			//参考物体
			string ref_obj = (string)data_row[5].ToString();
			if (ref_obj != "")
			{
				if (GameObject.Find(ref_obj) == null)
				{
					is_right = false;
				}
				if (!is_right)
				{
					if (Motion_Data.isEditor)
					{
						Debug.LogError(ErrorLocation.Locate("PIP", "REF_OBJ", row_id) + "，画中画参考物体出错！");
					}
					return false;
				}

			}
			_pipMotion.ref_obj = ref_obj;
			pipHashTable.Add("Ref_Obj", _pipMotion.ref_obj);

			//相机位置
			Vector3 pos_tmp = Motion_Compute.Vector3Conversion((string)data_row[6].ToString(), ref is_right);
			if (!is_right)
			{
				if (Motion_Data.isEditor)
				{
					Debug.LogError(ErrorLocation.Locate("PIP", "CAMERA_POSITION", row_id) + "，画中画相机位置出错！");
				}
				return false;
			}
			_pipMotion.camera_pos = pos_tmp;
			string cam_pos = pos_tmp.x + "," + pos_tmp.y + "," + pos_tmp.z;
			pipHashTable.Add("Camera_Pos", cam_pos);
			//cameraHashTable.Add("Camera_Elu", _pipMotion.camera_pos);

			//相机角度
			Vector3 elu_tmp = Motion_Compute.Vector3Conversion((string)data_row[7].ToString(), ref is_right);
			if (!is_right)
			{
				if (Motion_Data.isEditor)
				{
					Debug.LogError(ErrorLocation.Locate("PIP", "CAMERA_ELUERANGLE", row_id) + "，画中画相机角度出错！");
				}
				return false;
			}
			_pipMotion.camera_elu = elu_tmp;
			string cam_elu = elu_tmp.x + "," + elu_tmp.y + "," + elu_tmp.z;
			pipHashTable.Add("Camera_Elu", cam_elu);
			//cameraHashTable.Add("Camera_Elu", _pipMotion.camera_elu);

			//相机视野
			float camera_size_tmp = Motion_Compute.FloatConversion((string)data_row[8].ToString(), ref is_right);
			if (!is_right)
			{
				if (Motion_Data.isEditor)
				{
					Debug.LogError(ErrorLocation.Locate("PIP", "CAMERA_SIZE", row_id) + "，画中画相机的视野大小出错！");
				}
				return false;
			}
			_pipMotion.camera_size = camera_size_tmp;
			pipHashTable.Add("Camera_Size", _pipMotion.camera_size);

			//相机照准物体
			string focus_obj = (string)data_row[9].ToString();
			if (focus_obj != "")
			{
				if (GameObject.Find(focus_obj) == null)
				{
					is_right = false;
				}
				if (!is_right)
				{
					if (Motion_Data.isEditor)
					{
						Debug.LogError(ErrorLocation.Locate("PIP", "FOCUS_OBJ", row_id) + "，画中画照准物体出错！");
					}
					return false;
				}

			}
			_pipMotion.focus_obj = focus_obj;
			pipHashTable.Add("Focus_Obj", _pipMotion.focus_obj);

			//画中画是否消失
			string isDisappear = (string)data_row[10].ToString().ToUpper();
			if (isDisappear == "TRUE")
			{
				_pipMotion.isDisappear = true;
			}
			else if (isDisappear == "FALSE")
			{
				_pipMotion.isDisappear = false;
			}
			else
			{
				is_right = false;
				if (Motion_Data.isEditor)
				{
					Debug.LogError(ErrorLocation.Locate("PIP", "ISDISAPPEAR", row_id) + "，画中画控制消失出错！");
				}
				return false;
			}
			pipHashTable.Add("IsDisappear", _pipMotion.isDisappear);

			//记录画中画是否为方形
			try
			{
				string isSquare = (string)data_row[11].ToString().ToUpper();
				if (isSquare == "TRUE")
				{
					_pipMotion.isSquare = true;
				}
				else
				{
					_pipMotion.isSquare = false;
				}
				pipHashTable.Add("IsSquare", _pipMotion.isSquare);
			}
			catch
			{
				_pipMotion.isSquare = false;
				pipHashTable.Add("IsSquare", _pipMotion.isSquare);
			}

			//记录画中画相机的状态
			if (Motion_Data.isEditor && MotionPlatformData.currentMotion == MotionPlatformState.Teaching)
			{
				Type _t = Motion_TypeGet.MotionTypeGet("ReflectionManager");
				if (!MotionPlatformData.st_ReflectionManager && _t != null)
				{
					MotionPlatformData.st_ReflectionManager = (MonoBehaviour)GameObject.FindObjectOfType(_t);
				}
				if (MotionPlatformData.st_ReflectionManager && _t != null)
				{
					MethodInfo methodInfo = _t.GetMethod("ReflectionAddState");
					BindingFlags flag = BindingFlags.Public | BindingFlags.Instance;
					object[] para = new object[] { MotionPlatformData.excelName, MotionPlatformData.mainRowNumber + 2, "State_MinmapCamera", "Minmap_Camera", pipHashTable };
					methodInfo.Invoke(MotionPlatformData.st_ReflectionManager, flag, Type.DefaultBinder, para, null);
				}
			}

			motion_list.Add(_pipMotion);
			return true;
		}

	}


}
